package com.fzsf.drawingboard.ui

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import com.fzsf.drawingboard.adapter.MultiWritingAdapter
import com.fzsf.drawingboard.app.MyApplication
import com.fzsf.drawingboard.bluetooth.SimpleFZWritingPadCallBackListener
import com.fzsf.drawingboard.R
import kotlinx.android.synthetic.main.activity_multi_recycler.*

class MultiRecyclerActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_multi_recycler)
        initRecyclerView()
        setUpWritingPadCallBack()
    }

    private fun initRecyclerView() {
        recyclerView.layoutManager = GridLayoutManager(this, 4)
        recyclerView.adapter = MultiWritingAdapter(arrayListOf())
    }

    private fun setUpWritingPadCallBack() {
        MyApplication.getInstance().writingPad?.setFZWritingPadCallBackListener(object : SimpleFZWritingPadCallBackListener() {
            override fun onButtonClick(buttonType: String?) {
                runOnUiThread {
                    val multiWritingAdapter = recyclerView.adapter as MultiWritingAdapter
                    if (buttonType == "1") {
                        multiWritingAdapter.multiWritingBoard?.reSetBoard()
                    } else {
                        val crop2Save = multiWritingAdapter.multiWritingBoard?.crop2Save()
                        crop2Save?.let {
                            multiWritingAdapter.resultList.add(it)
                            recyclerView.adapter?.notifyDataSetChanged()
                            multiWritingAdapter.multiWritingBoard?.reSetBoard()
                        }
                    }
                }
            }

            override fun onWriting(x: Int, y: Int, z: Int, time: Long) {
                runOnUiThread {
                    (recyclerView.adapter as MultiWritingAdapter)?.multiWritingBoard?.writeByWritingPad(x, y, z)
                }
            }
        })
    }
}

